package com.xuecheng.content.controller;

import com.xuecheng.commons.model.dto.CourseAuditDto;
import com.xuecheng.commons.model.dto.CourseBaseDto;
import com.xuecheng.commons.model.dto.CourseDto;
import com.xuecheng.commons.model.dto.PageRequest;
import com.xuecheng.commons.model.vo.CourseBaseVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.content.domain.CourseBase;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 课程基本信息前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
@RequestMapping("/course")
public class CourseBaseController {

    @Autowired
    private CourseBaseService  courseBaseService;

    /**
     * 分页查询课程
     * @param pageRequest 分页请求 包括num size
     * @param courseDto 课程对象
     * @return 课程集合
     */
    @PostMapping("/list")
    public ResponseResult list(PageRequest pageRequest, @RequestBody CourseDto courseDto){
        return courseBaseService.courseList(pageRequest,courseDto);
    }

    /**
     * 课程文件上传方法
     * @param uploadFile 课程文件
     * @return 图片文件
     */
    @PostMapping("/upload")
    public ResponseResult upload(MultipartFile uploadFile) throws Exception {
        return courseBaseService.upload(uploadFile);
    }

    /**
     * 新增 课程信息
     * @param dto 课程信息填入的json
     * @return 课程
     * 响应数据  courseBaseId
     */
    @PostMapping
    public ResponseResult saveCourseBase(@RequestBody CourseBaseDto dto){
        return courseBaseService.saveCourseBase(dto);
    }

    /**
     * 根据id 查询
     * @param courseBaseId 基础课程id
     * @return vo
     */
    @GetMapping("/{courseBaseId}")
    public ResponseResult findById(@PathVariable("courseBaseId") Long courseBaseId){
        CourseBase courseBase = courseBaseService.getById(courseBaseId);
        CourseBaseVo courseBaseVo = BeanHelper.copyProperties(courseBase, CourseBaseVo.class);
        return ResponseResult.okResult(courseBaseVo);
    }

    /**
     * 更新课程信息
     * @param dto 课程原信息对象
     * @return vo
     */
    @PutMapping
    public ResponseResult updateCourseBase(@RequestBody CourseBaseDto dto){
        return courseBaseService.updateCourseBase(dto);
    }

    /**
     * 根据id删除课程
     * @param courseId 课程id
     * @return 删除vo
     */
    @DeleteMapping("/{courseId}")
    public ResponseResult deleteById(@PathVariable("courseId") Long courseId){
        return courseBaseService.deleteById(courseId);
    }

    /**
     * 提交课程审核
     * @param courseBaseId 基础课程id
     * @return vo
     */
    @GetMapping("/commit/{courseBaseId}")
    public ResponseResult commit(@PathVariable("courseBaseId") Long courseBaseId){
        return courseBaseService.commit(courseBaseId);
    }

    /**
     * 更新课程状态
     * @param courseAuditDto 课程审核对象
     * @return vo
     */
    @PostMapping("/approve")
    public ResponseResult approve(@RequestBody CourseAuditDto courseAuditDto){
        return courseBaseService.approve(courseAuditDto);
    }

    /**
     * 课程下架
     * @param courseId 课程id
     * @return null
     */
    @GetMapping("/offline/{courseId}")
    public ResponseResult offline(@PathVariable("courseId") Long courseId){
        return courseBaseService.offline(courseId);
    }
}
